package com.kupurui.asteacher.ui;

import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.transformer.BGAPageTransformer;
import com.kupurui.asteacher.transformer.TransitionEffect;
import com.kupurui.asteacher.view.HackyViewPager;

import java.util.List;

import butterknife.Bind;
import me.relex.photodraweeview.PhotoDraweeView;

/**
 * 显示大图
 */
public class ShowBigImageAty extends BaseAty {

    @Bind(R.id.viewpage)
    HackyViewPager mViewpage;
    @Bind(R.id.m_toolbar)
    Toolbar mToolbar;
    private List<String> mUrl;

    @Override
    public int getLayoutId() {
        return R.layout.show_big_image_aty;
    }

    @Override
    public void initData() {
        initToolbar(mToolbar,"查看大图");
        mUrl = getIntent().getExtras().getStringArrayList("pics");
        mViewpage.setAdapter(new DraweePagerAdapter());
        int position = getIntent().getIntExtra("position", 0);
        mViewpage.setCurrentItem(position);
        mViewpage.setPageTransformer(true, BGAPageTransformer.getPageTransformer(TransitionEffect.Accordion));
    }

    @Override
    public void requestData() {

    }


    public class DraweePagerAdapter extends PagerAdapter {


        @Override
        public int getCount() {
            return mUrl.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup viewGroup, int position) {
            final PhotoDraweeView photoDraweeView = new PhotoDraweeView(viewGroup.getContext());
            PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
            controller.setUri(Uri.parse(mUrl.get(position)));
            controller.setOldController(photoDraweeView.getController());
            controller.setControllerListener(new BaseControllerListener<ImageInfo>() {
                @Override
                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                    super.onFinalImageSet(id, imageInfo, animatable);
                    if (imageInfo == null) {
                        return;
                    }
                    photoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
                }
            });
            photoDraweeView.setController(controller.build());

            try {
                viewGroup.addView(photoDraweeView, ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return photoDraweeView;
        }
    }
}
